var express = require('express');
var cookie = require('cookie-parser');
var Base = require("./base");

class Page extends Base{
    constructor(){
        super();
        this.page = this.page.bind(this);
      
    }

    checkcookie(req,res,next){
        //console.log(req.cookies.cookie);
        if(typeof(req.cookies.cookie)=="undefined"){
            //res.redirect("/login.html");
            return true;
        }else{
            res.cookie("cookie", req.cookies.cookie, { expires: new Date(Date.now() + this.get_cookie_time()), httpOnly: true });
            return false;
        }
    }

    async page(req,res,next){

        var name = req.params.name;
        
        // console.log(name);
        // if(typeof(name)=="undefined")
        //     return res.render('index.html', { title: 'Express' });
        if(this.checkcookie(req,res))
            return res.redirect("/login.html");

        if(name == "index.html" || name == "index")
            return res.redirect("/");
       
        if(name.indexOf(".html") == -1)
            return res.redirect(name+".html");
        else
            return res.render(name,{title:"123"});
    }

}

module.exports = new Page();